<?php
session_start();

if(!isset($_SESSION['id'])){
	header('Location:login.php');
}
$downid=$_POST['typeid'];


$connetc=mysql_connect("localhost","root","");
mysql_select_db("shenji",$connetc);
mysql_query("set names utf8");
$array=array();
$sql="select * from jiekou where typeid=$downid order by id desc";
$result=mysql_query($sql);
if($result&&mysql_num_rows($result)){
	while($row=mysql_fetch_assoc($result)){
		$array[]=$row;
}

}
$sql2="select ename from type where typeid=$downid";
$result2=mysql_query($sql2);
$row2=mysql_fetch_assoc($result2);
$type=$row2['ename'];

foreach ($array as $item) {
	$licesneid=$item['licesneid'];
	$beizhu=$item['beizhu'];
	
	//$myfile = fopen("/var/www/html/upload/ruku/all.txt", "a+") or die("Unable to open file!");
	$myfile = fopen("/var/www/html/upload/ruku/".$type.".txt", "a+") or die("Unable to open file!");
	$arr=array($licesneid,$beizhu);
	$txt=implode(",",$arr);
	$txt1=$txt."\n";
	fwrite($myfile, $txt1);
	fclose($myfile);

}


header("Content-type:text/html;charset=utf-8");
//$file_name="all.txt";
$file_name=$type.".txt";


$file_name=iconv("utf-8","gb2312",$file_name);
$file_sub_path="/var/www/html/upload/ruku/";

$file_path=$file_sub_path.$file_name;


if(!file_exists($file_path)){
echo "没有该文件文件";
return;
}

$fp=fopen($file_path,"r");
$file_size=filesize($file_path);

Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length:".$file_size);
Header("Content-Disposition: attachment; filename=".$file_name);
$buffer=1024;
$file_count=0;

while(!feof($fp) && $file_count<$file_size){
$file_con=fread($fp,$buffer);
$file_count+=$buffer;
echo $file_con;
}
fclose($fp);
sleep(1);
unlink($file_path);

?>